package com.sci99.ex.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.sci99.ex.model.Course;
import com.sci99.ex.util.MyBatisUtil;

public class CourseService {
	static Logger log = Logger.getLogger(CourseService.class);

	public static Course getCourseById(Long id) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Course c = s.selectOne("getCourseById", id);
		s.close();
		return c;
	}

	public static List<Course> getCourseList() throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		List<Course> rs = s.selectList("getCourse");
		s.close();
		return rs;
	}

	public static List<Course> getCourseList(String name) throws Exception {
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("name", name);
		return getCourseList(args);
	}

	public static List<Course> getCourseList(Map<String, Object> args)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		List<Course> rs = s.selectList("getCourse", args);
		s.close();
		return rs;
	}

	public static List<Course> getCourseListByDepId(Long depId)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("depId", depId);
		List<Course> rs = s.selectList("getCourseListByDepId", args);
		s.close();
		return rs;
	}

	public static List<Course> getCourseListByEmpId(Long empId)
			throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("empId", empId);
		List<Course> rs = s.selectList("getCourseListByEmpId", args);
		s.close();
		return rs;
	}

	public static void saveCourse(Course c) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		if (c.getId() == null)
			s.insert("addCourse", c);
		else
			s.update("uptCourse", c);
		s.commit();
		s.close();
	}

	public static void delCourse(Course c) throws Exception {
		SqlSession s = MyBatisUtil.getSession();
		s.delete("delCourse", c);
		s.commit();
		s.close();
	}
}
